import {
  Controller,
  Post,
  Param,
  Request,
  UseGuards,
  Get,
} from '@nestjs/common';
import { AuthGuard } from '@nestjs/passport';
import { LikesService } from './likes.service';

@Controller('likes')
export class LikesController {
  constructor(private readonly likesService: LikesService) {}

  // 点赞或取消点赞动态
  @UseGuards(AuthGuard('jwt'))
  @Post('post/:postId')
  async togglePostLike(@Request() req, @Param('postId') postId: string) {
    try {
      const userId = parseInt(req.user.id);
      const result = await this.likesService.togglePostLike(userId, parseInt(postId));
      return {
        success: true,
        data: result,
        message: result.liked ? '点赞成功' : '取消点赞成功',
      };
    } catch (error) {
      return {
        success: false,
        data: null,
        message: '操作失败: ' + error.message,
      };
    }
  }

  // 获取动态的点赞状态和数量
  @UseGuards(AuthGuard('jwt'))
  @Get('post/:postId')
  async getPostLikeInfo(@Request() req, @Param('postId') postId: string) {
    try {
      const userId = parseInt(req.user.id);
      const result = await this.likesService.getPostLikeInfo(userId, parseInt(postId));
      return {
        success: true,
        data: result,
        message: '获取点赞信息成功',
      };
    } catch (error) {
      return {
        success: false,
        data: null,
        message: '获取点赞信息失败: ' + error.message,
      };
    }
  }
}